package com.cmwa.solution.business.infodisclosure.institution.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.infodisclosure.institution.entity.InstitutionVo;
import com.cmwa.solution.business.infodisclosure.institution.service.InstitutionService;
import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.utils.cached.DictionaryCached;

@Controller
@RequestMapping("/business/infodisclosure/institution")
public class InstitutionContorller extends BaseController{
	@Resource
	private InstitutionService institutionService;
	private final static String INSTITUTION_JSP_PATH = "business/infoDisclosure/institution/";
	
	@RequestMapping("/getInstitutionList")
	@ResponseBody
	public List<InstitutionVo> getInstitutionList(){
		return institutionService.getInstitutionList(null);
	}
	
	/**
	 * 跳转到列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/institutionInfoListView")
	public String institutionInfoListView(ModelMap model) {
		return INSTITUTION_JSP_PATH + "institutionInfoList";
	}
	
	@RequestMapping("/institutionInfoListPage")
	@ResponseBody
	public Page<InstitutionVo> institutionInfoListPage(SearchParam param) {
		Page<InstitutionVo> list = institutionService.institutionInfoListPage(param);
		return list;
	}
	
	@RequestMapping("/saveInstitutionInfo")
	public String saveInstitutionInfo(InstitutionVo institutionVo) {
		institutionService.insertInstitutionInfo(institutionVo);
		return redirectSuccess();
	}
	
	@RequestMapping("/institutions")
	@ResponseBody
	public List<InstitutionVo> institutions(@RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "limit", required=false) Integer limit) {
		List<InstitutionVo> instList = institutionService.getInstListLimit(name,limit);		
		return instList;
	}
	
	@RequestMapping("/checkRepeat")
	@ResponseBody
	public Result<Object> checkRepeat(@RequestParam(value = "instId",required=false) String instId ,
			@RequestParam(value="instCode",required=false) String instCode,
			@RequestParam(value="instSName",required=false) String instSName,
			@RequestParam(value="instLName",required=false) String instLName) throws Exception {
		boolean isRepeat = institutionService.checkRepeat(instId,instCode,instSName,instLName);
		Result<Object> result = new Result<>();
		result.setObject(isRepeat);
		return result;
	}
	
	@RequestMapping("/logicDeleteInstitutionInfoById")
	@ResponseBody
	public Result<?> logicDeleteInstitutionInfoById(@RequestParam("instId") String instId) {
		institutionService.logicDeleteInstitutionInfoById(instId);
		Result<Object> result = new Result<>();
		result.setSuccess(true);
		return result;
	}
	
	@RequestMapping("/institutionAddView")
	public String institutionAddView(Model model) {
		//查询角色
		List<DictionaryVo> instRolList = DictionaryCached.getDictionaryByType("INST_ROL_TYPE");
		model.addAttribute("instRolList", instRolList);
		return INSTITUTION_JSP_PATH + "institutionAdd";
	}
}
